#!/bin/bash
CA_CERT=/etc/kubernetes/pki/ca.crt
KUBECONFIG_PATH=/etc/kubernetes/kubeconfig
KUBECONFIG_YAML_PATH=/etc/kubernetes/yaml
sudo mkdir -p $KUBECONFIG_YAML_PATH
cat <<EOF | sudo tee $KUBECONFIG_YAML_PATH/kubelet-conf.yaml
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
cgroupDriver: systemd
authentication:
  anonymous:
    enabled: false
  webhook:
    enabled: true
  x509:
    clientCAFile: $CA_CERT
authorization:
  mode: Webhook
clusterDomain: cluster.local
clusterDNS:
  - 10.96.0.10
resolvConf: /etc/resolv.conf
runtimeRequestTimeout: 2m0s
EOF


cat <<EOF | sudo tee /usr/lib/systemd/system/kubelet.service
[Unit]
Description=Kubernetes Kubelet
Documentation=https://github.com/kubernetes/kubernetes
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=containerd.service

[Service]
ExecStart=/usr/local/bin/kubelet \\
    --bootstrap-kubeconfig=$KUBECONFIG_PATH/bootstrap-kubelet.kubeconfig  \\
    --kubeconfig=$KUBECONFIG_PATH/kubelet.kubeconfig \\
    --config=$KUBECONFIG_YAML_PATH/kubelet-conf.yaml \\
    --container-runtime-endpoint=unix:///run/containerd/containerd.sock  \\
    --node-labels=node.kubernetes.io/node=

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable --now kubelet

# sudo journalctl -u kube-apiserver
# sudo rm -rf /var/lib/kubelet/
# sudo rm -rf /var/lib/etcd/
# sudo /usr/local/bin/kubectl get nodes --kubeconfig /etc/kubernetes/kubeconfig/admin.kubeconfig